package com.lhh.adminlte.service.impl;

import com.lhh.adminlte.dao.*;
import com.lhh.adminlte.entity.*;
import com.lhh.adminlte.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: hanhui.liu
 * @Date: 2018/5/23 15:32
 * @Description: 用户相关
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserRoleMapper userRoleMapper;

    @Autowired
    RoleMenuMapper roleMenuMapper;

    @Autowired
    MenuMapper menuMapper;

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public List<Role> findRolesByUserId(int userId) {
        UserRoleExample userRoleExample = new UserRoleExample();
        userRoleExample.createCriteria().andUserIdEqualTo(userId);
        List<UserRole> userRoles = userRoleMapper.selectByExample(userRoleExample);
        List<Integer> ids = new ArrayList<>();
        for (UserRole userRole :
                userRoles) {
            ids.add(userRole.getRoleId());
        }
        RoleExample roleExample = new RoleExample();
        roleExample.createCriteria().andIdIn(ids);
        return roleMapper.selectByExample(roleExample);
    }

    @Override
    public List<Menu> findMenusByRoleId(int roleId, MenuType menuType) {
        RoleMenuExample roleMenuExample = new RoleMenuExample();
        roleMenuExample.createCriteria().andRoleIdEqualTo(roleId);
        List<RoleMenu> roleMenus = roleMenuMapper.selectByExample(roleMenuExample);
        List<Integer> ids = new ArrayList<>();
        for (RoleMenu roleMenu : roleMenus) {
            ids.add(roleMenu.getMenuId());
        }
        MenuExample menuExample = new MenuExample();
        MenuExample.Criteria menuExampleCriteria = menuExample.createCriteria();
        if (menuType != null) {
            menuExampleCriteria.andShowEqualTo(menuType.value);
        }
        menuExampleCriteria.andIdIn(ids);
        return menuMapper.selectByExample(menuExample);
    }

    @Override
    public List<Menu> findChildrenByParentId(int parentId, MenuType menuType) {
        MenuExample menuExample = new MenuExample();
        MenuExample.Criteria menuExampleCriteria = menuExample.createCriteria();
        menuExampleCriteria.andParentIdEqualTo(parentId);
        if (menuType != null) {
            menuExampleCriteria.andShowEqualTo(menuType.value);
        }
        return menuMapper.selectByExample(menuExample);
    }

    @Override
    public Menu findParentByChidId(int childId, MenuType menuType) {
        Menu menu = menuMapper.selectByPrimaryKey(childId);
        Menu parentMenu = menuMapper.selectByPrimaryKey(menu.getParentId());
        if (parentMenu.getShow() == menuType.value) {
            return parentMenu;
        }
        return null;

    }

    @Override
    public List<User> selectUsers(String nickName, String loginName, Integer status, int displayStart, int displayLength) {

        UserExample userExample = new UserExample();
        userExample.setOffset(displayStart);
        userExample.setLimit(displayLength);
        UserExample.Criteria criteria = userExample.createCriteria();
        if (!StringUtils.isEmpty(nickName)) {
            criteria.andNicknameLike(nickName);
        }
        if (!StringUtils.isEmpty(loginName)) {
            criteria.andLoginnameLike(loginName);
        }
        if (null != status && status > 0) {
            criteria.andStatusEqualTo(status);
        }
        return userMapper.selectByExample(userExample);
    }

    @Override
    public long countUser(String nickName, String loginName, Integer status) {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        if (!StringUtils.isEmpty(nickName)) {
            criteria.andNicknameLike(nickName);
        }
        if (!StringUtils.isEmpty(loginName)) {
            criteria.andLoginnameLike(loginName);
        }
        if (null != status && status > 0) {
            criteria.andStatusEqualTo(status);
        }
        return userMapper.countByExample(userExample);
    }
}
